package com.letou.ls.uitl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

/**
 * 开奖时间工具类
 *
 * @author libin
 *
 *         2016年6月12日
 */
@SuppressLint("SimpleDateFormat")
public class LotteryTimeUtils {
    /**
     * 黑龙江十一选五
     */
    private static List<String> fitFivelistTimes = new ArrayList<String>();
    /**
     * 黑龙江快乐十分
     */
    private static List<String> happyTenlistTimes = new ArrayList<String>();

    // 08:05:00 22:35:00
    private static void initFitFivelistTimes() {
        if (fitFivelistTimes.size() > 0) {
            return;
        }
        for (int i = 8; i <= 22; i++) {
            StringBuffer sb = new StringBuffer();
            if (i < 10) {
                sb.append("0").append(i + ":");
            } else {
                sb.append(i + ":");
            }
            for (int j = 5; j <= 55; j += 10) {
                if (i == 22 && j > 35) {
                    continue;
                }
                StringBuffer sb1 = new StringBuffer(sb.toString());
                if (j < 10) {
                    sb1.append("0" + j);
                } else {
                    sb1.append(j);
                }
                sb1.append(":00");
                fitFivelistTimes.add(sb1.toString());
            }
            
        }
    }
    
    // 08:45:00 22:35:00
    public static void initHappyTenlistTimes() {
        if (happyTenlistTimes.size() > 0) {
            return;
        }
        for (int i = 8; i <= 22; i++) {
            StringBuffer sb = new StringBuffer();
            if (i < 10) {
                sb.append("0").append(i + ":");
            } else {
                sb.append(i + ":");
            }
            for (int j = 5; j <= 55; j += 10) {
                if (i == 8) {
                    if (j > 35) {
                        StringBuffer sb1 = new StringBuffer(sb.toString());
                        if (j < 10) {
                            sb1.append("0" + j);
                        } else {
                            sb1.append(j);
                        }
                        sb1.append(":00");
                        happyTenlistTimes.add(sb1.toString());
                    }
                } else {
                    if (i == 22 && j > 35) {
                        continue;
                    }
                    StringBuffer sb1 = new StringBuffer(sb.toString());
                    if (j < 10) {
                        sb1.append("0" + j);
                    } else {
                        sb1.append(j);
                    }
                    sb1.append(":00");
                    happyTenlistTimes.add(sb1.toString());
                }
                
                
            }
            
        }
    }

    // 黑龙江十一选五 08:05:00 22:35:00
    public static String searchFitFivelistTimes(String nowTime) {
        initFitFivelistTimes();
        if (fitFivelistTimes.contains(nowTime)) {
            return "10:00";
        }
        String[] nowSpit = nowTime.split(":");
        int nowHours = Integer.valueOf(nowSpit[0]);
        int nowMinute = Integer.valueOf(nowSpit[1]);

        if (nowHours < 8 || nowHours == 8 && nowMinute < 5) {
            return "00:00";
        }
        if (nowHours > 22 || nowHours == 22 && nowMinute > 35) {
            return "00:00";
        }

        for (int i = 0; i <= fitFivelistTimes.size(); i++) {
            String time = fitFivelistTimes.get(i);
            String diffTime = subtractionTime(nowTime, time);
            if (!TextUtils.isEmpty(diffTime)) {
                return diffTime;
            }
        }
        return "00:00";
    }
    
    // 黑龙江快乐十分 08:45:00 22:35:00
    public static String searchHappyTenlistTimes(String nowTime) {
        initHappyTenlistTimes();
        if (happyTenlistTimes.contains(nowTime)) {
            return "10:00";
        }
        String[] nowSpit = nowTime.split(":");
        int nowHours = Integer.valueOf(nowSpit[0]);
        int nowMinute = Integer.valueOf(nowSpit[1]);

        if (nowHours < 8 || nowHours == 8 && nowMinute < 45) {
            return "00:00";
        }
        if (nowHours > 22 || nowHours == 22 && nowMinute > 35) {
            return "00:00";
        }

        for (int i = 0; i <= happyTenlistTimes.size(); i++) {
            String time = happyTenlistTimes.get(i);
            String diffTime = subtractionTime(nowTime, time);
            if (!TextUtils.isEmpty(diffTime)) {
                return diffTime;
            }
        }
        return "00:00";
    }
    
    public static String subtractionTime(String nowtime, String time) {
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        try {
            Date d1 = df.parse(time);
            Date d2 = df.parse(nowtime);
            long diff = d1.getTime() - d2.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes =
                    (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            long second = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)
                    - minutes * (1000 * 60)) / 1000;
            if (hours == 0 && minutes < 10 && minutes >= 0 && second >= 0) {
                Log.e("lb", "subtractionTime:" + "相差" + days + "天" + hours + "小时" + minutes + "分"
                        + second + "秒");
                return minutes + ":" + second;
            }

        } catch (Exception e) {}
        return "";
    }
}
